﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Resources;
using System.Reflection;
using Code.DAL;

namespace Code
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                DBCon c = new DBCon();
                List<string> lst = c.getDataBase(txtServer.Text, txtUserName.Text, txtPwd.Text);
                cbxDataTable.DataSource = lst;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public DataBase database = null;
        private void cbxDataTable_SelectedValueChanged(object sender, EventArgs e)
        {
            string db = cbxDataTable.SelectedValue.ToString();
            DBCon con = new DBCon();
            con.CreateTable(txtServer.Text, txtUserName.Text, txtPwd.Text, cbxDataTable.SelectedValue.ToString());
            lstTable.DataSource = con.DB.Tbs.OrderBy(x=>x.Name).ToList();
            lstTable.DisplayMember = "name";
            database = con.DB;
        }



        private void lstTable_Click(object sender, EventArgs e)
        {
            if (lstTable.SelectedValue == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(lstTable.SelectedValue.ToString()))
            {
                return;
            }
            string tableName = ((Table)lstTable.SelectedValue).Name;
            Table tb = database.Tbs.Single(x => x.Name == tableName);
            //判断表中是否有主键
            if (tb.Columns.Count(x=>x.IsKey==true)==0 || tb.Columns.Count(x=>x.IsKey==true)>1)
            {
                MessageBox.Show("对不起，代码生成器只支持一个主键");
                return;
            }
            CreateCode cc = new CreateCode();
            rtbProc.Text = cc.CreateProc(tb);
            rtbEntity.Text = cc.CreateEntity(tb);
            rtbIDAL.Text = cc.CreateIDAL(tb);
            rtbDAL.Text = cc.CreateDAL(tb);
            rtbIBaseBLL.Text = cc.CreateIBaseBLL(tb);
            rtbIBaseDAL.Text = cc.CreateIBaseDAL(tb);
            rtbIBLL.Text = cc.CreateIBLL(tb);
            rtbBLL.Text = cc.CreateBLL(tb);
            rtbTools.Text = cc.CreateTools(tb);
        }

        private void 生成代码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lstTable.SelectedValue == null)
                return;
            if (string.IsNullOrEmpty(lstTable.SelectedValue.ToString()))
            {
                return;
            }
            string tableName = ((Table)lstTable.SelectedValue).Name;
            Table tb = database.Tbs.Single(x => x.Name == tableName);
            //判断表中是否有主键
            if (tb.Columns.Count(x => x.IsKey == true) == 0 || tb.Columns.Count(x => x.IsKey == true) > 1)
            {
                MessageBox.Show("对不起，代码生成器只支持一个主键");
                return;
            }
            CreateCode cc = new CreateCode();
            string EntityFileName = cc.FirstCapital(tableName) + ".cs";
            string ProcFileName = cc.FirstCapital(tableName) + ".sql";
            string IDALFileName = "I" + cc.FirstCapital(tableName) + "DAL.cs";
            string DALFileName = cc.FirstCapital(tableName) + "DAL.cs";
            string IBaseBLLFileName = "IBaseBLL.cs";
            string IBaseDALFileName = "IBaseDAL.cs";
            string IBLLFileName = "I" + cc.FirstCapital(tableName) + "BLL.cs";
            string BLLFileName = cc.FirstCapital(tableName) + "BLL.cs";
            string ToolsName="Tools.cs";
            CreateFile("Entity", EntityFileName, cc.CreateEntity(tb));
            CreateFile("SQL", ProcFileName, cc.CreateProc(tb));
            CreateFile("IDAL", IDALFileName, cc.CreateIDAL(tb));
            CreateFile("DAL", DALFileName, cc.CreateDAL(tb));
            CreateFile("IBLL", IBaseBLLFileName, cc.CreateIBaseBLL(tb));
            CreateFile("IDAL", IBaseDALFileName, cc.CreateIBaseDAL(tb));
            CreateFile("IBLL", IBLLFileName, cc.CreateIBLL(tb));
            CreateFile("BLL", BLLFileName, cc.CreateBLL(tb));
            CreateFile("DAL", ToolsName, cc.CreateTools(tb));

        }


        private void CreateFile(string folder, string fileName, string content)
        {
            string fileFolder = path + "/" + folder;
            if (!Directory.Exists(fileFolder))
            {
                Directory.CreateDirectory(fileFolder);
            }
            string filePath=fileFolder+"/"+fileName;
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            FileStream s= File.Create(filePath);
            Byte [] data= System.Text.Encoding.Default.GetBytes(content);
            s.Write(data, 0, data.Length);
            s.Flush();
            s.Close();
            

        }
        private  string path
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["File"];
            }
        }

    }
}
